<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\CheckTopic;
use App\Admin\Repositories\Topic;
use App\Admin\RowActions\Delete;
use App\Admin\RowActions\Fail;
use App\Admin\RowActions\Pass;
use App\Models\Topic as TopicModel;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Tab;


class TopicController extends AdminController
{
    public function index(Content $content)
    {
        $tab = Tab::make();
        $tab->add('全部', $this->grid(1), true);
        $tab->add('未审核', $this->grid(0));

        return $content
            ->title('帖子')
            ->description('列表')
            ->body($tab->withCard());
    }

    protected function grid($status)
    {
        return Grid::make(new Topic(['member', 'topicTag', 'topicTheme']), function (Grid $grid) use ($status) {
            if ($status == 0) {
                $grid->model()->where('status', '=', 0);
            }

            $map = getAuthMiniProgramMap(Admin::user()->id);
            $grid->model()->whereIn('mini_type', array_keys($map));

            $grid->model()->orderByDesc('created_at');

            if (request()->get('_view_') !== 'list') {
                // 设置自定义视图
                $grid->view('admin.topic.index');
                $grid->actions([new Delete(TopicModel::class)]);
                $grid->actions([new Pass(TopicModel::class)]);
                $grid->actions([new Fail(TopicModel::class)]);
                $grid->setActionClass(Grid\Displayers\Actions::class);
            }
            $grid->batchActions([
                new CheckTopic('批量审核通过', 1),
                new CheckTopic('批量审核不通过', 2)
            ]);

            $grid->filter(function (Grid\Filter $filter) use ($map) {
                $filter->panel();
                $filter->expand();
                $filter->equal('mini_type')->select($map)->width(2);
                $filter->equal('id', '帖子ID')->width(2);
                $filter->equal('member_id', 'UID')->width(2);
                $filter->like('member.nickname', '用户名')->width(2);
                $filter->equal('is_hot', '热门')->select(['0' => '否', '1' => '是'])->width(2);
                $filter->like('topicTheme.name', '话题名')->width(2);
                $filter->between('created_at')->datetime()->width(4);
            });

            $grid->column('id');
            $grid->column('mini_type')->display(function ($miniType) use($map) {
                return $miniType ? $map[$miniType] : '';
            });
            $grid->column('member_id');
            $grid->column('member.nickname');
            $grid->column('member.avatar');
            $grid->column('name');
            $grid->column('files')->display(function ($pictures) {
                return explode(',', $pictures);
            })->image();
            $grid->column('title');
            $grid->column('comment');
            $grid->column('status');
            $grid->column('created_at');
            $grid->column('updated_at');
            $grid->column('is_hot')->switch();

            $grid->column('topicTag.name');
            $grid->column('topicTheme');

            $grid->disableDeleteButton();
            $grid->disableEditButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableFilterButton();
            $grid->disableRefreshButton();
            $grid->disableBatchDelete();
            $grid->disableCreateButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Topic(), function (Form $form) {
            $form->display('id');
            $form->radio('is_hot')->options(['否', '是'])->default('0');
        });
    }
}
